package cn.picclife.mwx.manager.sys.service.impl;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import cn.picclife.mwx.common.core.util.SpringContextUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.picclife.mwx.manager.base.BaseResult1;
import cn.picclife.mwx.manager.base.ResponseHeadDto;
import cn.picclife.mwx.manager.sys.model.po.TokenRes;
import cn.picclife.mwx.manager.sys.service.AccessService;
import cn.picclife.mwx.manager.util.RedisUtil;

@Service
public class AccessServiceImpl implements AccessService {
	
	private static final Log log = LogFactory.getLog(AccessServiceImpl.class);
	private final static String TOKEN_URL = "/slxt-web/collect/compareSysTime";
	private static Map<String, String> urlMap;
	
	static{
		urlMap = new HashMap<String, String>();
		urlMap.put("/slxt-web/collect/saveCollect", "/slxt-web/collect/saveCollect");
		urlMap.put("/slxt-web/collect/updateCollect", "/slxt-web/collect/updateCollect");
		urlMap.put("/slxt-web/collect/addTrack", "/slxt-web/collect/addTrack");
		urlMap.put("/slxt-web/collect/compareSysTime", "/slxt-web/collect/compareSysTime");
		urlMap.put("/slxt-web/tipMatch/querytargetTip", "/slxt-web/tipMatch/querytargetTip");
		urlMap.put("/slxt-web/uploadFile2Repostry", "/slxt-web/uploadFile2Repostry");
		urlMap.put("/slxt-web/collect/compareHtml", "/slxt-web/collect/compareHtml");
		urlMap.put("/slxt-web/collect/isNotCompare", "/slxt-web/collect/isNotCompare");
		urlMap.put("/slxt-web/collect/downloadStaticFile", "/slxt-web/collect/downloadStaticFile");
	}
    private RedisUtil redisUtil =SpringContextUtil.getBean(RedisUtil.class);
	/**
	 * @description 验证路径

	 * @author saizhiyi
	 * @param url
	 * @return
	 */
	@Override
	public String checkUrl(String url) {
		String flag = "0";// url标识：0-获取token、1-过滤接口、2-非过滤接口
		if(TOKEN_URL.equals(url)){
			log.info("获取token");
			flag = "0";
		} else if(urlMap.containsKey(url)) {
			flag = "1";
		} else {
			flag = "2";
		}
		return flag;
	}

	/**
	 * @description 获取token 
	 * @author saizhiyi
	 * @return
	 */
	@Override
	public TokenRes getToken() {
		TokenRes token = new TokenRes();
		log.info("获取token开始...");
		//生成token
		String access_token = UUID.randomUUID().toString().replace("-", "");
		// redis中token已失效，保存新token并返回
		redisUtil.setStr(access_token, access_token);
		redisUtil.setExpire(access_token, 1200000);
		log.info("token存入redis完成");
		token.setResponseHeadDto(new ResponseHeadDto(BaseResult1.SUCCESS, "查询token成功"));
		token.setAccessToken(access_token);
		return token;
	}

	/**
	 * @description 验证token 
	 * @author saizhiyi
	 * @param accessToken
	 * @param time
	 * @param md5Token
	 * @param systemId
	 * @return
	 * @throws Exception
	 */
	@Override
	public boolean checkToken(String accessToken, String time, String md5Token, String systemId) throws Exception {
		String reids_access_token = "";
		Object reids_access_token_boj = redisUtil.getStr(accessToken);
		if(reids_access_token_boj != null){
			reids_access_token = reids_access_token_boj.toString();
		}
		log.info("redis取出的数据：" + reids_access_token);
//		String md5Str_1 = this.MD5Encryption(reids_access_token + time);
		String md5Str_1 = this.sha256Encode(reids_access_token + time);
		int size = md5Str_1.length();
//		String md5Str_2 = this.MD5Encryption(md5Str_1.substring(size/2) + md5Str_1.substring(0, (size/2)));
		String md5Str_2 = this.sha256Encode(md5Str_1.substring(size/2) + md5Str_1.substring(0, (size/2)));
		log.info("加密之后的token为：" + md5Str_2);
		if(md5Token.equals(md5Str_2)){
			return true;
		}
		log.info("token验证失败");
		return false;
	}
	
	/**
	 * @description 加密方法 
	 * @author saizhiyi 
	 * @param sr
	 * @return
	 * @throws Exception
	 */
//	private String MD5Encryption(String sr) throws Exception {
//		String md5Str = DigestUtils.md5DigestAsHex(sr.getBytes());
//		return md5Str;
//	}

	
	public String sha256Encode(String sr){
        return DigestUtils.sha256Hex(sr.getBytes());
    }


}
